package boerse.main.StockPriceProvider;

import boerse.main.Asset.Share;
import boerse.main.Exceptions.NegativeValueException;

import java.util.TimerTask;


public class RandomStockPriceProvider extends StockPriceProvider {

	private double max = 12;
	private double min = -12;
	
	public RandomStockPriceProvider() {
		super();
        startUpdate();
	}

	@Override
	protected void updateShareRates()  {
		
		for (int i = 0; i < allShare.length; i++) {
			if (allShare[i] == null) {		
				continue;
			}
			
			long price = allShare[i].getPrice();
			double x = Math.random()*(max-min+1)+min;
			try {
				allShare[i].setPrice(price + (long) x);
			} catch (NegativeValueException e) {
				return;
			}
			
		}
		
	}

	@Override
	protected void updateShareRate(String shareName) {
		
		Share share = getShare(shareName);
		long price = share.getPrice();
		double x = Math.random()*(max-min+1)+min;
		try {
			share.setPrice(price + (long) x);
		} catch (NegativeValueException e) {
			return;
		}
		
	}

	@Override
	public void startUpdate() {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                RandomStockPriceProvider.this.updateShareRates();
            }
        }  , 1000, 2000);
	}
}
